package com.ruoyi.suggestion.service.impl;

import java.math.BigDecimal;
import java.util.Collections;
import java.util.List;
import java.util.Map;

import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.suggestion.domain.DashboardStats;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
import com.ruoyi.suggestion.mapper.CommunitySuggestionMapper;
import com.ruoyi.suggestion.domain.CommunitySuggestion;
import com.ruoyi.suggestion.service.ICommunitySuggestionService;

/**
 * 社区建议Service业务层处理
 * 
 * @author xianyu
 * @date 2025-04-02
 */
@Service
public class CommunitySuggestionServiceImpl implements ICommunitySuggestionService 
{
    @Autowired
    private CommunitySuggestionMapper communitySuggestionMapper;

    /**
     * 查询社区建议
     * 
     * @param id 社区建议主键
     * @return 社区建议
     */
    @Override
    public CommunitySuggestion selectCommunitySuggestionById(Long id)
    {
        return communitySuggestionMapper.selectCommunitySuggestionById(id);
    }

    /**
     * 查询社区建议列表
     * 
     * @param communitySuggestion 社区建议
     * @return 社区建议
     */
    @Override
    public List<CommunitySuggestion> selectCommunitySuggestionList(CommunitySuggestion communitySuggestion)
    {
        return communitySuggestionMapper.selectCommunitySuggestionList(communitySuggestion);
    }

    /**
     * 新增社区建议
     * 
     * @param communitySuggestion 社区建议
     * @return 结果
     */
    @Override
    public int insertCommunitySuggestion(CommunitySuggestion communitySuggestion)
    {
        communitySuggestion.setCreateTime(DateUtils.getNowDate());
        return communitySuggestionMapper.insertCommunitySuggestion(communitySuggestion);
    }

    /**
     * 修改社区建议
     * 
     * @param communitySuggestion 社区建议
     * @return 结果
     */
    @Override
    public int updateCommunitySuggestion(CommunitySuggestion communitySuggestion)
    {
        communitySuggestion.setUpdateTime(DateUtils.getNowDate());
        return communitySuggestionMapper.updateCommunitySuggestion(communitySuggestion);
    }

    /**
     * 批量删除社区建议
     * 
     * @param ids 需要删除的社区建议主键
     * @return 结果
     */
    @Override
    public int deleteCommunitySuggestionByIds(Long[] ids)
    {
        return communitySuggestionMapper.deleteCommunitySuggestionByIds(ids);
    }

    /**
     * 删除社区建议信息
     * 
     * @param id 社区建议主键
     * @return 结果
     */
    @Override
    public int deleteCommunitySuggestionById(Long id)
    {
        return communitySuggestionMapper.deleteCommunitySuggestionById(id);
    }

    @Override
    public List<TableDataInfo> getDashboardStats() {
        // 查询数据
        Map<String, Object> statsMap = communitySuggestionMapper.getDashboardStats();

        // 创建返回的对象
        DashboardStats dashboardStats = new DashboardStats();

        // 设置基础指标
        dashboardStats.setAnnualSuggestions(safeLongToInt((Long) statsMap.get("annualSuggestions")));
        dashboardStats.setMonthlySuggestions(safeLongToInt((Long) statsMap.get("monthlySuggestions")));
        dashboardStats.setDailySuggestions(safeLongToInt((Long) statsMap.get("dailySuggestions")));
        dashboardStats.setSuccessRate(((BigDecimal) statsMap.get("successRate")).doubleValue());
        dashboardStats.setActiveUsers(safeLongToInt((Long) statsMap.get("activeUsers")));

        // 计算趋势指标
        Long currentYear = (Long) statsMap.get("currentYear");
        Long lastYear = (Long) statsMap.get("lastYear");
        dashboardStats.setAnnualTrend(calculateTrend(currentYear, lastYear));

        Long currentMonth = (Long) statsMap.get("currentMonth");
        Long lastMonth = (Long) statsMap.get("lastMonth");
        dashboardStats.setMonthlyTrend(calculateTrend(currentMonth, lastMonth));

        // 示例值，如果需要从数据库获取，请调整 SQL 查询和 Mapper 方法
        dashboardStats.setDailyTrend(8.7);
        dashboardStats.setActiveTrend(2.1);

        // 将 DashboardStats 转换为 TableDataInfo
        TableDataInfo tableDataInfo = new TableDataInfo();
        tableDataInfo.setMsg("Success");
        tableDataInfo.setCode(HttpStatus.OK.value());
        tableDataInfo.setRows(Collections.singletonList(dashboardStats));
        tableDataInfo.setTotal(1L); // 假设只有一个 DashboardStats 对象

        // 返回包含单个 TableDataInfo 实例的列表
        return Collections.singletonList(tableDataInfo);
    }

    @Override
    public List<TableDataInfo> suggestVote() {
        return communitySuggestionMapper.suggestVote();
    }

    // 安全转换 Long 到 Integer
    private Integer safeLongToInt(Long value) {
        return value != null ? value.intValue() : 0;
    }

    // 计算趋势
    private Double calculateTrend(Long current, Long previous) {
        if (previous == null || previous == 0) {
            return 0.0;
        }
        return ((current - previous) / (double) previous) * 100;
    }
}
